﻿using System;
using System.Diagnostics;
using System.Collections.Generic;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Emgu.CV;
using Emgu.CV.Structure;
using Emgu.Util;
using Emgu.CV.UI;
using GeometryProcessing;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
using System.IO;

namespace CCD
{
    public partial class ProcessConsole2 : Form
    {
        #region AUXI

        bool InImgBound(Point p, IImage img)
        {
            return (p.X >= 0 && p.Y >= 0 && p.X < img.Size.Width && p.Y < img.Size.Height);
        }

        #endregion

        #region GLOBAL VARIABLE

        Image<Rgb, Byte> imgAve;
        Image<Rgb, Byte> imgLower;
        Image<Rgb, Byte> imgUpper;
        Image<Rgb, Byte> imgDiff;

        Image<Gray, Byte> mask;

        #endregion

        #region GENERAL PROCESS

        private Image<Rgb, Byte> preProcess(Image<Rgb, Byte> src)
        {
            return src.Convert<Gray,Byte>().Convert<Rgb, Byte>();
        }

        #endregion

        public ProcessConsole2()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private Rgb RgbSum(Rgb a, Rgb b)
        {
            return new Rgb(a.Red + b.Red, a.Green + b.Green, a.Blue + b.Blue);
        }

        private Rgb RgbMul(Rgb a, double k)
        {
            return new Rgb(a.Red * k, a.Green * k, a.Blue * k);
        }

        private Rgb RgbAbsSub(Rgb a, Rgb b)
        {
            return new Rgb(Math.Abs( a.Red - b.Red ), Math.Abs( a.Green - b.Green ), Math.Abs(a.Blue - b.Blue));
        }

        private void button1_Click(object sender, EventArgs e)
        {

        }

        private void button2_Click(object sender, EventArgs e)
        {
            string[] fns = Directory.GetFiles(tbTargetPath.Text);

            foreach (var fn in fns)
            {

                Image<Rgb, Byte> procImg = new Image<Rgb, Byte>(fn);
                procImg = preProcess(procImg);

                var imgRst = imgAve.Clone();
                
                //===============  Version 1   ===========
                // Show Ave
                //imgRst = imgAve.AbsDiff(procImg);

                //===============  Version 2   ===========
                // MAX and MIN
                var imgRst1 = imgLower.Cmp(procImg, Emgu.CV.CvEnum.CMP_TYPE.CV_CMP_GT);
                var imgRst2 = imgUpper.Cmp(procImg, Emgu.CV.CvEnum.CMP_TYPE.CV_CMP_LT);
                imgRst = imgRst1.Or(imgRst2);

                // Gray, Morph
                var imgRstGray = imgRst.Convert<Gray, Byte>();
                imgRstGray._ThresholdBinary(new Gray(trackBar1.Value * 10), new Gray(255));
                imgRstGray._Erode(3);
                imgRstGray._Dilate(3);
                imgRstGray._And(mask);


                // SHow Image
                Image<Rgb, Byte> showImg = imgRst.Clone();
                showImg = (imgRstGray.Convert<Rgb, Byte>()).Mul(0.8).Add(procImg.Mul(0.2));

//                imageBox1.Image = showImg;
                imageBox1.Image = imgRstGray;

                MessageBox.Show("");
            }
        }

        Image<Gray, Byte> returnImg;
        private void button3_Click(object sender, EventArgs e)
        {
            returnImg = new Image<Gray, Byte>(imgAve.Size);
            BlockDefine wnd = new BlockDefine(tbBlockName.Text, "data\\model\\modelname1\\blocks\\blockname1\\mask.bmp", imgAve);
            wnd.Show();

        }

        private void button4_Click(object sender, EventArgs e)
        {
//            mask = new Image<Gray, byte>(tbBlockName.Text);
            mask = returnImg;

            imageBox1.Image = mask;
        }

        private void button1_Click_1(object sender, EventArgs e)
        {
            TrainConsole train = new TrainConsole("pic\\test", "data\\model\\modelname1");

            train.Show();
        }

        private void trackBar1_Scroll(object sender, EventArgs e)
        {

        }



    }
}
